//
//  UIView+Extension.m
//  黑马微博2期
//
//  Created by apple on 14-10-7.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "UIView+Extension.h"

@implementation UIView (Extension)

- (void)setK_x:(CGFloat)k_x {
    CGRect frame = self.frame;
    frame.origin.x = k_x;
    self.frame = frame;
}

- (void)setK_y:(CGFloat)k_y {
    CGRect frame = self.frame;
    frame.origin.y = k_y;
    self.frame = frame;
}
- (CGFloat)k_x {
    return self.frame.origin.x;
}

- (CGFloat)k_y {
    return self.frame.origin.y;
}

- (void)setK_centerX:(CGFloat)k_centerX {
    CGPoint center = self.center;
    center.x = k_centerX;
    self.center = center;
}

- (CGFloat)k_centerX {
    return self.center.x;
}

- (void)setK_centerY:(CGFloat)k_centerY {
    CGPoint center = self.center;
    center.y = k_centerY;
    self.center = center;
}


- (CGFloat)k_centerY
{
    return self.center.y;
}

- (void)setK_width:(CGFloat)k_width
{
    CGRect frame = self.frame;
    frame.size.width = k_width;
    self.frame = frame;
}

- (void)setK_height:(CGFloat)k_height
{
    CGRect frame = self.frame;
    frame.size.height = k_height;
    self.frame = frame;
}

- (CGFloat)k_height
{
    return self.frame.size.height;
}

- (CGFloat)k_width
{
    return self.frame.size.width;
}

- (void)setK_size:(CGSize)k_size
{
    CGRect frame = self.frame;
    frame.size = k_size;
    self.frame = frame;
}

- (CGSize)k_size
{
    return self.frame.size;
}

- (void)setK_origin:(CGPoint)k_origin
{
    CGRect frame = self.frame;
    frame.origin = k_origin;
    self.frame = frame;
}

- (CGPoint)k_origin
{
    return self.frame.origin;
}


- (CGFloat)k_bottom {
    return self.frame.origin.y + self.frame.size.height;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setK_bottom:(CGFloat)k_bottom {
    CGRect frame = self.frame;
    frame.origin.y = k_bottom - frame.size.height;
    self.frame = frame;
}


- (CGFloat)k_top {
    return self.frame.origin.y;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setK_top:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}
- (BOOL)isShowingOnKeyWindow
{
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    // 以主窗口左上角为坐标原点, 计算self的矩形框
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect winBounds = keyWindow.bounds;
    
    // 主窗口的bounds 和 self的矩形框 是否有重叠
    BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
    
    return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}

@end
